package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.dao.PictureSheetOfMerchandiseDao;
import com.example.demo.pojo.*;
import com.example.demo.service.BackgrounduserService;
import com.example.demo.service.ManageGoods;
import com.example.demo.service.RepertorygoodsService;
import com.example.demo.service.WarehouseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@Api(value = "大仓库管理",tags = "仓库管理接口")
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private RepertorygoodsService repertorygoodsService;
    @Autowired
    private BackgrounduserService backgrounduserService;
    @Autowired
    private ManageGoods manageGoods;
    @Autowired
    private CommodityListDao commodityListDao;
    @Autowired
    PictureSheetOfMerchandiseDao pictureSheetOfMerchandiseDao;
    //查找所有仓库
    @ApiOperation(value = "查找所有仓库",notes = "查找所有仓库")
    @RequestMapping(value = "/findall",method = RequestMethod.GET)
    public String selectAll(){
      List<Warehouse> warehouses=warehouseService.selectAll();
       return JSONArray.toJSONString(warehouses);
    }

    //添加仓库
    @ApiOperation(value = "添加仓库",notes ="添加仓库" )
    @RequestMapping(value = "/insertWarehouse",method = RequestMethod.GET)
    @ApiImplicitParams({
              @ApiImplicitParam(name = "name",value ="仓库名字" ),
              @ApiImplicitParam(name = "x",value ="经度" ),
              @ApiImplicitParam(name = "y",value ="纬度" ),
              @ApiImplicitParam(name = "accountNumber",value ="仓库管理员名字" )
    })
    @ApiResponses({@ApiResponse(code = 1,message = "操作成功"),@ApiResponse(code = 0,message = "操作失败"),@ApiResponse(code = 3,message = "权限不足")})
    public String insertWarehouse(String name,String x,String y,String accountNumber){
        Backgrounduser backgrounduser=                                                                                                                                                                                                                                                                                                                                                                                                         backgrounduserService.selectByName(accountNumber);
        Warehouse warehouse=new Warehouse();
        warehouse.setName(name);
        warehouse.setX(x);
        warehouse.setY(y);
        warehouse.setUcid(backgrounduser.getBackgrounduserid());
        int falg=warehouseService.insert(warehouse);
        if (falg==1){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
    //删除仓库(root)
    @RequestMapping(value = "/delectWarehouse",method = RequestMethod.POST)
    @ApiOperation(value = "删除仓库",notes ="删除仓库" )
    @ApiImplicitParam(name = "repertoryid",value ="仓库id" )
    public String delete(int repertoryid){
        int falg=warehouseService.deleteByPrimaryKey(repertoryid);
        if (falg==1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    //修改仓库(root)
    @RequestMapping(value = "/updateWarehouse",method = RequestMethod.POST)
    @ApiOperation(value = "修改仓库",notes ="修改仓库" )
    @ApiImplicitParam(name = "repertoryid",value ="仓库id" )
    public String update(int repertoryid,String name,String x,String y,int ucid){
        Warehouse warehouse=new Warehouse();
        warehouse.setRepertoryid(repertoryid);
        warehouse.setUcid(ucid);
        warehouse.setName(name);
        warehouse.setX(x);
        warehouse.setY(y);
        int falg=warehouseService.updateByPrimaryKey(warehouse);
        if (falg==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    //查找仓库(root)
    @RequestMapping(value = "/selectWarehouse",method = RequestMethod.POST)
    @ApiOperation(value = "查找仓库",notes ="查找仓库" )
    @ApiImplicitParam(name = "name",value ="仓库名字" )
    public String select(String name){
        Warehouse warehouse=warehouseService.selectByName(name);
        return JSONArray.toJSONString(warehouse);
    }

    //查找仓库详情（root）
    @RequestMapping(value = "/selectgoods",method = RequestMethod.POST)
    @ApiOperation(value = "查找仓库商品",notes ="查找仓库商品" )
    @ApiImplicitParam(name = "repertoryid",value ="仓库id" )
    public String selectRepertory(int repertoryid){
        List<Repertorygoods> repertorygoods=repertorygoodsService.selectBy(repertoryid);
        return JSONArray.toJSONString(repertorygoods);
    }
    //添加仓库商品（仓库管理员）
    @RequestMapping(value = "/insertGoods",method = RequestMethod.GET)
    @ApiOperation(value = "添加仓库商品",notes ="添加仓库商品" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsid",value ="商品id" ),
            @ApiImplicitParam(name = "stock",value ="库存" ),
    })
    public String insertGoods(Integer goodsid, Integer stock, HttpSession session, Model model){
        System.out.println("我是商品i=========="+goodsid);
        Backgrounduser backgrounduser= (Backgrounduser) session.getAttribute("houtaiUser");
        Warehouse warehouse=warehouseService.selectByUserId(backgrounduser.getBackgrounduserid());
        Repertorygoods repertorygoods=new Repertorygoods();
        repertorygoods.setStock(stock);
        repertorygoods.setGoodsid(goodsid);
        repertorygoods.setRepertoryid(warehouse.getRepertoryid());
        int flag=repertorygoodsService.insertSelective(repertorygoods);
        if (flag==1){
              model.addAttribute("tishi","添加成功");
        }else {
              model.addAttribute("tishi","添加失败");
        }
        selectGoos(model);
        return "product";
    }

    @RequestMapping("/backGoods")
    public String selectGoos(Model model){
        List<CommodityList> commodityLists =manageGoods.findAllByCommoditylist();
        model.addAttribute("commodityList",commodityLists);
        return "product";
    }
    //查看本仓库（仓库管理员）
    @RequestMapping(value = "/selectGoods",method = RequestMethod.GET)
    @ApiOperation(value = "根据管理员查看本仓库商品",notes ="根据管理员查看本仓库商品" )
    public String selectUserWarehouse( HttpSession session,Model model){
        Backgrounduser backgrounduser= (Backgrounduser) session.getAttribute("houtaiUser");
        Warehouse warehouse=warehouseService.selectByUserId(backgrounduser.getBackgrounduserid());
        List<Repertorygoods> repertorygoods=repertorygoodsService.selectBy(warehouse.getRepertoryid());
        model.addAttribute("cangku",repertorygoods);
        List<CommodityList> lists=new ArrayList<>();
        List<CommodityList> commodityLists=new ArrayList<>();
        for (int i = 0; i <repertorygoods.size() ; i++) {
           CommodityList commodityList=commodityListDao.selectByPrimaryKey(repertorygoods.get(i).getGoodsid());
           if (repertorygoods.get(i).getIss()==1){
               //缺货
               commodityLists.add(commodityList);
           }else {
               //有货
               lists.add(commodityList);
           }
        }
        for (int i = 0; i <lists.size() ; i++) {
            int B=lists.get(i).getGoodsid();
            PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
            pictureSheetOfMerchandise.setIss(1);
            pictureSheetOfMerchandise.setGoodsid(B);
            List<PictureSheetOfMerchandise> PI=pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise);
            lists.get(i).setPicturepath(PI.get(0).getPictureaddress());
        }
        for (int i = 0; i <commodityLists.size() ; i++) {
            int B=commodityLists.get(i).getGoodsid();
            PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
            pictureSheetOfMerchandise.setIss(1);
            pictureSheetOfMerchandise.setGoodsid(B);
            List<PictureSheetOfMerchandise> PI=pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise);
            commodityLists.get(i).setPicturepath(PI.get(0).getPictureaddress());
        }
        model.addAttribute("quehuoshangpin",commodityLists);
        model.addAttribute("wodeshangpin",lists);
        System.out.println("你好");
        return "wode";
    }
    @RequestMapping("/warehouseDo")
    public String warehouseDo(Model model){
      // try{
           List<Warehouse> warehouses=warehouseService.selectAll();
           for (Warehouse w:warehouses
                   ) {
               Backgrounduser backgrounduser= backgrounduserService.selectByPrimaryKey(w.getUcid());
               w.setUsernName(backgrounduser.getAccountnumber());
           }
           model.addAttribute("warehouses",warehouses);
           return "message";
      // }catch (Exception e){
     //      return "服务器异常";
      // }
    }
    @RequestMapping("/updWareUserNameDo")
    public String updWareUserNameDo(int userId,Model model){
        Backgrounduser backgrounduser= backgrounduserService.selectByPrimaryKey(userId);
        System.out.println(backgrounduser.getBackgrounduserid());
        model.addAttribute("backgrounduser",backgrounduser);
        return "updWareUserName";
    }
    @RequestMapping("/updWareUserName")
    public String updWareUserName(String userName,int userId){
        System.out.println(userName+"噶"+userId+"ga");
       int i= warehouseService.updWareUserName(userName,userId);
        return "redirect:/warehouseDo";
    }
    //删除仓库(root)
    @RequestMapping(value = "/delectWarehouseId")
    @ApiOperation(value = "删除仓库",notes ="删除仓库" )
    @ApiImplicitParam(name = "repertoryid",value ="仓库id" )
    public String deleteId(int repertoryid){
        int falg=warehouseService.deleteByPrimaryKey(repertoryid);
     return "redirect:/warehouseDo";
    }
    @RequestMapping("/addWareDo")
    public String addWareDo(Model model){
       List<Backgrounduser> selectBackgroundusers= backgrounduserService.selectBackgrounduser();
        for (Backgrounduser b:selectBackgroundusers
             ) {
            System.out.println(b.getBackgrounduserid());
        }
        model.addAttribute("selectBackgroundusers",selectBackgroundusers);
        return "addWare";
    }
    @RequestMapping("/addWare")
    private String addWare(Warehouse warehouse){
        int falg=warehouseService.insert(warehouse);
        return "redirect:/warehouseDo";
    }
}
